package com.mavenq.fly.websocket.config;


import com.mavenq.fly.websocket.interceptor.TokenHandshakeInterceptor;
import com.mavenq.fly.websocket.handler.BroadcastWebSocketHandler;
import com.mavenq.fly.websocket.handler.EchoWebSocketHandler;
import com.mavenq.fly.websocket.handler.RoutingWebSocketHandler;
import com.mavenq.fly.websocket.service.WSConnectionService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final TokenHandshakeInterceptor tokenInterceptor;
    private final WSConnectionService connectionService;
    private final EchoWebSocketHandler echoHandler;
    private final BroadcastWebSocketHandler broadcastHandler;
    private final RoutingWebSocketHandler routingHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 路由处理器：/ws/route/{token}（推荐）
        registry.addHandler(routingHandler, "/ws/route/{token}")
                .addInterceptors(tokenInterceptor)
                .setAllowedOrigins("*");

        // 广播处理器：/ws/broadcast/{token}
        registry.addHandler(broadcastHandler, "/ws/broadcast/{token}")
                .addInterceptors(tokenInterceptor)
                .setAllowedOrigins("*");

        // 回声处理器：/ws/echo/{token}
        registry.addHandler(echoHandler, "/ws/echo/{token}")
                .addInterceptors(tokenInterceptor)
                .setAllowedOrigins("*");
    }
}